package genedit.model;

import genedit.gui.GProjectTree;

import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.swing.SwingUtilities;
import javax.swing.tree.TreeNode;

/**
 * Osnovna jedinica za trajno čuvanje podataka. Projekat sadrži pakete u kojima
 * se nalaze dijagrami.
 * 
 * @author Nikola
 * @version 1.2 18/12/2013
 */
public class Project extends HierarchyElement {

	private static final long serialVersionUID = 1L;

	/**
	 * Referenca na roditelja (u slučaju projekta Workspace).
	 */
	private transient HierarchyElement parent = null;

	/**
	 * Brojač za generisanje podrazumevanog imena dece ovog čvora.
	 */
	private int elementCount = 0;

	/**
	 * Ime projekta.
	 */
	private String name;

	/**
	 * Autor projekta.
	 */
	private String author;

	/**
	 * Vreme kreiranja projekta.
	 */
	private Date dateCreated;

	/**
	 * Vreme poslednjeg čuvanja projekta.
	 */
	private Date dateLastModified;

	/**
	 * Putanja do projekta.
	 */
	private String path;

	// Promenljiva koja će se možda uklanjati. Neka stoji za sada.
	private boolean isSaved;

	/**
	 * Lista paketa koji predstavljaju decu ovog čvora u stablu.
	 */
	private List<Package> packages;

	public Project(HierarchyElement parent) {
		super();
		this.parent = parent;
		packages = new java.util.ArrayList<Package>();
	}

	public Project(HierarchyElement parent, String name) {
		super();
		this.parent = parent;
		this.name = name;
		packages = new java.util.ArrayList<Package>();
	}

	public HierarchyElement getParent() {
		return parent;
	}

	public void setParent(HierarchyElement parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String newAuthor) {
		author = newAuthor;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public Date getDateLastModified() {
		return dateLastModified;
	}

	public void rename(String name) {
		this.name = name;
	}

	public java.util.Collection<Package> getPackages() {
		if (packages == null)
			packages = new java.util.ArrayList<Package>();
		return packages;
	}

	public java.util.Iterator<Package> getIteratorPackages() {
		if (packages == null)
			packages = new java.util.ArrayList<Package>();
		return packages.iterator();
	}

	public void setPackages(java.util.Collection<Package> newPackages) {
		removeAllPackages();
		for (java.util.Iterator<Package> iter = newPackages.iterator(); iter.hasNext();)
			addPackages((Package) iter.next());
	}

	public void addPackages(Package newPackage) {
		if (newPackage == null)
			return;
		if (this.packages == null)
			this.packages = new java.util.ArrayList<Package>();
		if (!this.packages.contains(newPackage))
			this.packages.add(newPackage);
		SwingUtilities.updateComponentTreeUI(GProjectTree.getInstance().getTree());
	}

	public void removePackages(Package oldPackage) {
		if (oldPackage == null)
			return;
		if (this.packages != null)
			if (this.packages.contains(oldPackage))
				this.packages.remove(oldPackage);
		SwingUtilities.updateComponentTreeUI(GProjectTree.getInstance().getTree());
	}

	public void removeAllPackages() {
		if (packages != null)
			packages.clear();
		SwingUtilities.updateComponentTreeUI(GProjectTree.getInstance().getTree());
	}

	@Override
	public TreeNode getChildAt(int childIndex) {
		return packages.get(childIndex);
	}

	@Override
	public int getChildCount() {
		return packages.size();
	}

	@Override
	public int getIndex(TreeNode node) {
		return packages.indexOf(node);
	}

	@Override
	public boolean getAllowsChildren() {
		return true;
	}

	@Override
	public boolean isLeaf() {
		return false;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Enumeration children() {
		return Collections.enumeration(packages);
	}

	public String toString() {
		return name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public boolean isSaved() {
		return isSaved;
	}

	@Override
	public void setSaved(boolean saved) {
		this.isSaved = saved;
	}

	@Override
	public void removeChild(HierarchyElement he) {
		removePackages((Package) he);
		super.removeChild(he);
	}

	public int getElementCount() {
		return ++elementCount;
	}

	public void setElementCount(int elementCount) {
		this.elementCount = elementCount;
	}
}